package com.lhz.services;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lhz.dao.GoodDao;
import com.lhz.dao.GoodTypeDao;
import com.lhz.dao.mybatis.GoodsDaoMybatis;
import com.lhz.pojos.Good;
import com.lhz.pojos.Staff;
import com.lhz.utils.GetUUID;


@Service
@Transactional
public class GoodService {

	@Autowired
	GoodDao goodDao;
	
	@Autowired
	GoodTypeDao goodTypeDao;
	
	@Autowired
	GoodsDaoMybatis mGoodsDao;
	
	@Autowired
	HttpServletRequest request;
	
	/**
	 * 根据条件查询商品
	 * @param pageNow
	 * @param pageSize
	 * @param goodsName
	 * @param goodsType
	 * @return
	 */
	public Map<String,Object> findGoodByCondition(Integer pageNow,Integer pageSize,String goodsName,Integer goodsType){
		Map<String,Object> map=new HashMap<>();
		PageHelper.startPage(pageNow,pageSize,true);
		List<Good> findGoodByCondition = mGoodsDao.findGoodByCondition(goodsName,goodsType);
		PageInfo<Good> page = new PageInfo<>(findGoodByCondition);
		map.put("goodsList",findGoodByCondition);
		map.put("goodsTypeList", goodTypeDao.findAll());
		map.put("total", page.getTotal());
		if(goodsType!=null) {
			map.put("goodsType", goodsType);
		}
		return map;
	}
	
	/**
	 * 修改商品
	 * @param goodsName
	 * @param goodsId
	 * @param goodsPrice
	 * @return
	 */
	public Map<String,Object> updateGoods(String goodsName,Integer goodsId,Double goodsPrice,String goodsDetail){
		Map<String,Object> map=new HashMap<>();
		Good goods1=goodDao.findByGoodsName(goodsName);
		Good goods2=goodDao.findOne(goodsId);
		if(goods1!=null&&goods1.getGoodsId()!=goods2.getGoodsId()) {
			map.put("msg", "商品名称："+goodsName+"已存在");
			return map;
		}
		Good good=goodDao.findOne(goodsId);
		good.setGoodsName(goodsName);
		good.setPrice(new BigDecimal(goodsPrice));
		good.setDetail(goodsDetail);
		map.put("result", "修改成功");
		return map;
	}
	
	/**
	 * 返回good对象
	 * @param goodsId
	 * @return
	 */
	public Good getGood(Integer goodsId) {
		return goodDao.findOne(goodsId);
	}
	
	/**
	 * 更新商品状态
	 * @param goodsId
	 * @param status
	 * @return
	 */
	public Map<String,Object> updateGoodsStatus(Integer goodsId,String status){
		Map<String,Object> map=new HashMap<>();
		Good good=goodDao.findOne(goodsId);
		good.setStatus(status);
		map.put("result", "修改成功");
		return map;
	}
	
	/**
	 * 上传商品图片
	 * @param file
	 * @return
	 * @throws IOException 
	 */
	@SuppressWarnings("unused")
	public Map<String,Object> uploadImg(MultipartFile file,Integer goodsId) throws IOException{
		Map<String,Object> map=new HashMap<>();
		if(file==null||file.isEmpty()||file.getSize()<=0) {
  			map.put("msg", "没有图片!");
 			return map;
 		}
 		//判断上传文件格式和上传的大小
 		String oldFileName=file.getOriginalFilename();//获取文件名
  		String[] a = oldFileName.split("\\.");
 		if (!(a[1].equals("jpg") || a[1].equals("bmp") || 
 				a[1].equals("gif") ||a[1].equals("png")||
 				a[1].equals("jpeg"))) {
 			 map.put("msg","图片格式有误!");
 			 return map;
 		 }
		// 获取目录
		String uploads =  request.getSession().getServletContext().getRealPath("/upload");//获取服务器指定文件存取路径  
		System.out.println(uploads);
		File dir = new File(uploads);
		if (!dir.exists()) {
			dir.mkdirs();//创建路径
		}
		String newFileName = GetUUID.getNewFileName(oldFileName);//给文件生成一个唯一文件名追加上后缀
		String fullFileName = uploads + "\\" + newFileName;
		File writeFile=new File(fullFileName);
		writeFile.createNewFile();
        file.transferTo(writeFile);  //转存文件 
        //查出数据库中的文件进行删除
        Good good = goodDao.findOne(goodsId);
        if(good!=null) {
        	File file2 = new File(uploads +good.getImage());
        	if(file2.exists()) {
        		file2.delete();
        	}
        	String goodImg=good.getImage();
            good.setImage(newFileName);
        	map.put("result", "上传成功!");
        }else {
        	map.put("msg", "上传失败!");
        }
		return map;
	}
	
	/**
	 * 删除商品
	 * @param goodsId
	 * @return
	 */
	public Map<String,Object> deleteGoods(Integer goodsId){
		Map<String,Object> map=new HashMap<>();
		Good good=goodDao.findOne(goodsId);
		if(good==null) {
			map.put("result", "删除失败");
			map.put("msg", "找不到商品");
			return map;
		}
		String uploads =  request.getSession().getServletContext().getRealPath("/upload");
		File file=new File(uploads+"\\"+good.getImage());
		if(file.exists()) {
			file.delete();
		}
		goodDao.delete(good);
		map.put("result", "删除成功");
		return map;
	}
	
	
	
}
